# -*- coding: utf-8 -*-

from  runner import RuleRunner, PlayBookRunner, CommandRunner
from  inventory import BaseInventory


def  TestpalyRunner():
        host_data = [
            {
                "hostname": "web2",
                "ip": "",
                "username": "root",
            },
        ]
        inventory = BaseInventory(host_data)
        runner = PlayBookRunner(inventory)

        ret = runner.run("/opt/playbook/slb_web1.yml", {'host':"web2", 'user': 'root'})
        status_data = ret["status"]
        perform_status = False
        for v in status_data.values():
            if v["failures"] == 0 and v["unreachable"] == 0 and v["skipped"] == 0:
                perform_status = True
            else:
                perform_status = False
        if perform_status:
            print("更新成功！")

def  TestruleRunner():
        host_data = [
            {
                "hostname": "web2",
                "ip": "",
                "username": "root",
            },
        ]
        inventory = BaseInventory(host_data)
        runner = RuleRunner(inventory)
        ret = runner.run("/etc/ansible/site.yml", {'host':"web2", 'user': 'root', 'my_data':'1111'}, "test")
        status_data = ret["status"]
        perform_status = False
        for v in status_data.values():
            if v["failures"] == 0 and v["unreachable"] == 0 and v["skipped"] == 0:
                perform_status = True
            else:
                perform_status = False
        if perform_status:
            print("更新成功！")

def TestCommandRunner():
#        host_data = [
#            {
#                "hostname": "web2",
#                "ip": "10.132.4.181",
#                "port": 22,
#                "username": "root",
#                "password": "JieXin#wEb2@234678",
#            },
#        ]
        host_data = {
           "web2": {
               "hosts": [
                   {
                       'username': 'root',
                       'hostname': '10.132.4.181',
                       'ip': '10.132.4.181',
                   }
               ]
           }
        }
        inventory = BaseInventory(host_data)
        runner = CommandRunner(inventory)

        res = runner.execute('pwd', 'web2')
        #print(res.results_raw)
        #print(res.results_command)
        if res.results_raw["ok"] != {}:
            print("执行成功！")

if __name__ == "__main__":
    #TestruleRunner()
    #TestpalyRunner()
    TestCommandRunner()
